package com.wande.dataplatform.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.bo.JobInstanceBo;
import com.wande.dataplatform.domain.vo.JobInstanceVO;
import com.wande.dataplatform.service.IOpsService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 运维管理控制器
 *
 * @author wande
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataplatform/ops")
public class OpsController extends BaseController {

    private final IOpsService opsService;

    /**
     * 查询作业实例列表
     */
    @SaCheckPermission("dataplatform:ops:list")
    @GetMapping("/instances")
    public TableDataInfo<JobInstanceVO> listInstances(JobInstanceBo bo, PageQuery pageQuery) {
        return opsService.queryInstanceList(bo, pageQuery);
    }

    /**
     * 查询作业实例详情
     */
    @SaCheckPermission("dataplatform:ops:query")
    @GetMapping("/instances/{instanceId}")
    public R<JobInstanceVO> getInstanceDetail(@PathVariable Long instanceId) {
        return R.ok(opsService.queryInstanceDetail(instanceId));
    }

    /**
     * 查询作业实例树形结构
     */
    @SaCheckPermission("dataplatform:ops:query")
    @GetMapping("/instances/{instanceId}/tree")
    public R<JobInstanceVO> getInstanceTree(@PathVariable Long instanceId) {
        return R.ok(opsService.queryInstanceTree(instanceId));
    }

    /**
     * 获取实例日志
     */
    @SaCheckPermission("dataplatform:ops:query")
    @GetMapping("/instances/{instanceId}/logs")
    public R<String> getInstanceLogs(@PathVariable Long instanceId) {
        return R.ok(opsService.getInstanceLogs(instanceId));
    }

    /**
     * 下载实例日志
     */
    @SaCheckPermission("dataplatform:ops:query")
    @GetMapping("/instances/{instanceId}/logs/download")
    public void downloadInstanceLogs(@PathVariable Long instanceId, HttpServletResponse response) throws IOException {
        byte[] logData = opsService.downloadInstanceLogs(instanceId);
        
        response.setContentType("text/plain;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=instance_" + instanceId + "_logs.txt");
        response.getOutputStream().write(logData);
        response.getOutputStream().flush();
    }

    /**
     * 重跑作业实例
     */
    @SaCheckPermission("dataplatform:ops:rerun")
    @Log(title = "运维管理", businessType = BusinessType.UPDATE)
    @PostMapping("/instances/{instanceId}/rerun")
    public R<Long> rerunInstance(@PathVariable Long instanceId) {
        Long newInstanceId = opsService.rerunInstance(instanceId);
        return R.ok("重跑成功，新实例ID: " + newInstanceId, newInstanceId);
    }

    /**
     * 停止作业实例
     */
    @SaCheckPermission("dataplatform:ops:stop")
    @Log(title = "运维管理", businessType = BusinessType.UPDATE)
    @PostMapping("/instances/{instanceId}/stop")
    public R<Void> stopInstance(@PathVariable Long instanceId) {
        opsService.stopInstance(instanceId);
        return R.ok("停止成功");
    }

    /**
     * 批量停止作业实例
     */
    @SaCheckPermission("dataplatform:ops:stop")
    @Log(title = "运维管理", businessType = BusinessType.UPDATE)
    @PostMapping("/instances/batch-stop")
    public R<Integer> batchStopInstances(@RequestBody List<Long> instanceIds) {
        Integer successCount = opsService.batchStopInstances(instanceIds);
        return R.ok("成功停止 " + successCount + " 个实例", successCount);
    }

    /**
     * 批量重跑作业实例
     */
    @SaCheckPermission("dataplatform:ops:rerun")
    @Log(title = "运维管理", businessType = BusinessType.UPDATE)
    @PostMapping("/instances/batch-rerun")
    public R<Integer> batchRerunInstances(@RequestBody List<Long> instanceIds) {
        Integer successCount = opsService.batchRerunInstances(instanceIds);
        return R.ok("成功重跑 " + successCount + " 个实例", successCount);
    }

    /**
     * 删除作业实例
     */
    @SaCheckPermission("dataplatform:ops:remove")
    @Log(title = "运维管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/instances/{instanceId}")
    public R<Void> deleteInstance(@PathVariable Long instanceId) {
        opsService.deleteInstance(instanceId);
        return R.ok("删除成功");
    }

    /**
     * 批量删除作业实例
     */
    @SaCheckPermission("dataplatform:ops:remove")
    @Log(title = "运维管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/instances")
    public R<Integer> batchDeleteInstances(@RequestBody List<Long> instanceIds) {
        Integer successCount = opsService.batchDeleteInstances(instanceIds);
        return R.ok("成功删除 " + successCount + " 个实例", successCount);
    }
}
